#include <buola/gui/ctoolkit.h>
#include <buola/gui/x11/cxrender.h>
#include <buola/gui/x11/ccomposite.h>
#include <buola/gui/x11/crandr.h>
#include <buola/gui/x11/cfixes.h>
#include <buola/gui/x11/cx11window.h>
#include <buola/gui/x11/cglxcontext.h>
#include <buola/app/capp.h>
#include <buola/gl/copenglgc.h>

using namespace buola;

class CManagerApp : public CApp
{
    bool Start()
    {
        CApp::Start();
        gui::x11::CXRender::InitExtension();
        gui::x11::CComposite::InitExtension();
        gui::x11::CRandR::InitExtension();
        gui::x11::CFixes::InitExtension();
        
        mWindow=new gui::x11::CX11Window(nullptr,gui::x11::get_default_x11_display());
        mWindow->Create(nullptr,{100,100},{200,200});
        mWindow->Map(true);
        msg_info() << "window is " << mWindow->GetX11ID() << "\n";
        mGLContext=new gui::x11::CGLXContext(mWindow);
        mGLContext->MakeCurrent(mWindow);
        gl::clear_color(img::CColor(0.7,0.3,0.5));
        glClear(GL_COLOR_BUFFER_BIT);

        
        mOpenGLGC=new gl::COpenGLGC(mGLContext);
        mOpenGLGC->MakeCurrent(mWindow);
        mOpenGLGC->SetExposeSize({200,200});
        mOpenGLGC->Path().MoveTo(0,0);
        mOpenGLGC->Path().LineTo(100,0);
        mOpenGLGC->Path().LineTo(100,100);
        mOpenGLGC->Path().Close();
        mOpenGLGC->FillPath();
        mGLContext->SwapBuffers();
        
        return true;
    }
    
    gui::x11::CX11Window *mWindow;
    gui::x11::CGLXContext *mGLContext;
    gl::COpenGLGC *mOpenGLGC;
};

int main(int pNArg,char **pArgs)
{
    buola_init(pNArg,pArgs);
    
    {
        CManagerApp lManager;
        //gui::CToolkit::SetWindowFactory(&lManager);
    
        lManager.Run();
    }
    
    return buola_finish();
}
